package com.aerozhonghuan.logisticsmall.modules.home;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aerozhonghuan.foundation.eventbus.EventBusManager;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.DB.DBAdapter;
import com.aerozhonghuan.logisticsmall.modules.common.IMainView;
import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.aerozhonghuan.logisticsmall.modules.common.entity.ProductImg;
import com.aerozhonghuan.logisticsmall.modules.common.event.TabNumberChangedEvent;
import com.aerozhonghuan.logisticsmall.utils.CommunityTool;
import com.aerozhonghuan.logisticsmall.utils.CommunityUtils;
import com.aerozhonghuan.logisticsmall.utils.StatisticsConstant;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenLin on 2019/5/20 16:22.Email&WeChat:7877409@163.com
 */
public class HomePageListGridAdapter extends BaseAdapter {


    Activity mContext;
    List<GoodsObject> mlist = new ArrayList<GoodsObject>();
    private RequestOptions options;

    public HomePageListGridAdapter(Activity context) {
        mContext = context;
        options = new RequestOptions().placeholder(R.mipmap.ic_goods_placehoder_little).error(R.mipmap.ic_goods_placehoder_little);
    }

    public void setData(List<GoodsObject> list) {
        if (list == null || list.size() == 0) {
            mlist.clear();
        } else {
            mlist.clear();
            mlist.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mlist == null || mlist.size() == 0) {
            return 0;
        }
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.type_item, null);
            holder = new Holder();
            holder.icon = (ImageView) convertView.findViewById(R.id.type_icon);
            holder.textView = (TextView) convertView.findViewById(R.id.type_title);
            holder.priceView = (TextView) convertView.findViewById(R.id.type_price);
            holder.disCountPriceView = (TextView) convertView.findViewById(R.id.discount_price);
            holder.buyView = (ImageView) convertView.findViewById(R.id.buy);
            holder.buyView.setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.into_gouwuche));
            holder.productInfoView = convertView.findViewById(R.id.product_info);
            holder.quantityAvailable = (TextView) convertView.findViewById(R.id.quantity_available);
            holder.productSize = (TextView) convertView.findViewById(R.id.product_size);
            holder.jiaobiaoRight = (ImageView) convertView.findViewById(R.id.jiaobiao_right);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (mlist == null) {
            return null;
        }
        final GoodsObject product = mlist.get(position);
        if (product != null) {
            holder.icon.setImageResource(R.mipmap.def_img);
            List<ProductImg> pImageList = product.getpImageList();
            int productImgId = 0;
            if (pImageList != null && pImageList.size() > 0) {
                for (ProductImg productImg : pImageList) {
                    if (productImg.is_thumbnail == 1) {
                        productImgId = productImg.imgid;
                        break;
                    } else {
                        productImgId = productImg.imgid;
                    }
                }
            }
            String imgUrl = CommunityTool.changeImageId(productImgId, true, false);
            Glide.with(mContext).load(imgUrl).apply(options).into(holder.icon);
            holder.textView.setText(product.getBaseName());
            holder.priceView.setText("￥ " + product.getPrice() / 100f);
            holder.disCountPriceView.setVisibility(View.GONE);//原价
            holder.productSize.setVisibility(View.GONE);//数量
            holder.jiaobiaoRight.setVisibility(View.INVISIBLE);
            holder.buyView.setVisibility(View.VISIBLE);//购买按钮
            holder.quantityAvailable.setVisibility(View.GONE);//已抢光文字
            holder.buyView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int productSize = CommunityUtils.addProductToGouwuche(mContext, product, new DBAdapter(mContext), 2);
                    int pGouwucheSize = product.getIntoGouwucheSize();
                    if (pGouwucheSize <= product.getQuantityAvailable()) {
                        int[] start_location = new int[2];// 一个整型数组，用来存储按钮的在屏幕的X、Y坐标
                        view.getLocationInWindow(start_location);// 这是获取购买按钮的在屏幕的X、Y坐标（这也是动画开始的坐标）
                        //mAnimImg = new ImageView(mContext);// buyImg是动画的图片，我的是一个小球（R.drawable.sign）
                        //mAnimImg.setImageDrawable(holder.icon.getDrawable());// 设置buyImg的图片
                        EventBusManager.post(new TabNumberChangedEvent(IMainView.TAB_CART_FRAGMENT, productSize));
                        StatisticsConstant.getInstance().ClickActionParams(4, 101, String.valueOf(product.getPvsid()));
                    } else {
                        StatisticsConstant.getInstance().ClickActionParams(5, 101, String.valueOf(product.getPvsid()));
                        ToastUtils.showShort("库存不足了，先买这么多吧!");
                    }
                }
            });

            holder.productInfoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //GoodsObject object = (GoodsObject) v.getTag();
                    Intent intent = new Intent(mContext,
                            GoodsDetailsActivity.class);
                    intent.putExtra("pvsid", product.getPvsid());
                    LogUtils.i(product);
                    mContext.startActivity(intent);
                }
            });
            holder.productInfoView.setTag(product);
        }
        return convertView;
    }

    class Holder {
        ImageView icon;
        TextView textView;
        TextView priceView;
        TextView disCountPriceView;
        ImageView buyView;
        View productInfoView;
        TextView quantityAvailable;
        TextView productSize;
        ImageView jiaobiaoRight;
    }
}
